package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Order;
import cn.itcast.service.OrderListService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/order")
public class OrderListController {
    @Reference
    private OrderListService orderListService;

    /**分页查询*/
    @GetMapping(path = "/findPage")
    public Result findPage(OrderQueryPageBean orderQueryPageBean) {
        //分页查询检查组信息
        try {
            PageResult pageResult = orderListService.findAllOrderByPage(orderQueryPageBean);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**新增预约与修改预约一起*/
    @PostMapping(path="/submit")
    public Result addOrder(@RequestBody Order order){
        Boolean result;
        //根据是否存在id,判断是新增还是修改
        if (null!=order.getId()){
            //修改前
            Order orderInit = orderListService.findOrderById(order.getId());
            //修改后
            result = orderListService.updateOrder(order);
            if (!result){
                return new Result(false,MessageConstant.ORDER_FAIL);
            }
            orderListService.updateOrderReservations(orderInit);//减一操作
            orderListService.updateOrderReservations(order);//加一操作
        }else {
            //新增
            result=orderListService.saveOrder(order);
            if (!result){
                return new Result(false,MessageConstant.ORDER_FAIL);
            }
            orderListService.updateOrderReservations(order);//加一操作
        }
        //操作失败
        return new Result(true,MessageConstant.ORDER_FAIL);
    }

    /**编辑预约*/
    @GetMapping(path="/{id}")
    public Result editOrder(@PathVariable("id") String id){
        Order order = orderListService.findOrderById(id);
        if (null == order) {
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
        return new Result(true, MessageConstant.ORDER_SUCCESS,order);
    }

    /**是否到诊*/
    @GetMapping(path="/updateArrival")
    public Result updateArrival(String id,Integer arrival){
        Boolean result;
        //更新到诊
        result = orderListService.updateOrderArrivalById(id, arrival);
        if (result){
            //更新成功
            return new Result(result,MessageConstant.ORDERSETTING_SUCCESS);
        }
        //更新失败
        return new Result(result,MessageConstant.ORDERSETTING_FAIL);
    }



}
